# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import pickle
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_percentage_error

def draw_result(load_1999_predict, scaler):
    path = r'../附件/处理后的数据/天负荷数据_1999.xlsx'
    data = pd.read_excel(path, index_col=0)
    load_1999 = data['energy_load']
    date = range(1,32)
    
    load_1999_predict = scaler.inverse_transform(load_1999_predict)
    
    mape = mean_absolute_percentage_error(load_1999, load_1999_predict)
    print('1999年拟合数据和实际数据的 MAPE 为： ', mape, '%')
    fig = plt.figure()
    plt.plot(date, load_1999, 'b', label='real dataset')
    plt.plot(date, load_1999_predict, 'r--', label='fitting dataset')
    plt.xlabel('Date')
    plt.ylabel('Load/MW')
    fig.savefig(r'../图片/实际数据_拟合数据.png')
    plt.legend()
    plt.show()


if __name__ == '__main__':
    data_1999 = pd.read_excel(r'../附件/中间数据/天负荷数据_1999_ml.xlsx')
    # 1998 年 最后一天数据
    data_begin = pd.read_excel(r'../附件/中间数据/天负荷数据_1997_1998_ml.xlsx')
    data_begin = data_begin.iloc[-1, [0,1,2,4,5,6,7,8]]
    
    lr = pickle.load(open(r'../附件/中间数据/lr_model.pkl', 'rb'))
    scaler = pickle.load(open(r'../附件/中间数据/load_scaler.pkl', 'rb'))
    
    y = lr.predict(data_begin.values.reshape((1,-1))) 
    y = float(y)
    load_data = data_begin.loc[['load1(t-3)', 'load1(t-2)',
                                    'load1(t-1)', 'load1(t)']].values
    
    load_1999_predict = list(load_data)
    load_1999_predict.append(y)

    
    # 预测 1999 年 1月 的 31 天数据
    for i in range(1, 31):
        
        is_holiday = data_1999.iloc[i, 0]
        is_weekend = data_1999.iloc[i, 1]
        temperature = data_1999.iloc[i, 2]
        load_data = load_1999_predict[-5:]

         
        data_for_predict = [is_holiday, is_weekend, temperature] + load_data
        data_for_predict = np.array(data_for_predict).reshape((1,-1))
        y = lr.predict(data_for_predict)
        load_1999_predict.append(float(y))
    
    draw_result(load_1999_predict[4:], scaler)
    
    
